# 批量发送邮件(工资条)
import smtplib
from openpyxl import load_workbook
from email.mime.text import MIMEText
from email.header import Header

smtp_obj = smtplib.SMTP('smtp.163.com')
smtp_obj.login('18735816507@163.com', '授权密码')

# 编辑内容
wb = load_workbook('../1.office_cases/01.py_excel/02.py_excelFile/工资条.xlsx')
sh = wb.active

table_head = '<tr>'  # 创建表头
for index, row in enumerate(sh.iter_rows()):
    # 第一行数据
    if index == 0:
        for col in row:
            table_head += f'<td>{col.value}</td>'
        table_head += '</tr>'
        continue
    else:
        if row[1].value == None:
            break
        table_info = '<tr>'
        for col in row:
            table_info += f'<td>{col.value}</td>'
        table_info += '</tr>'

        name = row[1].value
        address = row[8].value
        msg_info = f'''
                <h3>您好，{name}：</h3>
                <p>请查收2023年12月的工资详情</p>
                <table border="1">
                    {table_head}
                    {table_info}
                </table>
            '''
        # 编写文本内容
        msg_body = MIMEText(msg_info, 'html', 'utf-8')
        msg_body['From'] = Header('人事部', 'utf-8')  # 来源
        msg_body['Subject'] = Header('xx公司12月份工资详情', 'utf-8')  # 主题

        # 发送邮件
        smtp_obj.sendmail("18735816507@163.com", ["xxacker@163.com"], msg_body.as_string())
        print('发送成功！')



'''
您好：{name} 请查收本月的工资详情：
<table>
	<tr>
		table head<td>工号</td>
		<td>姓名</td>
		<td>基本工资</td>
		<td>保险</td>
		<td>总金额</td>
	</tr>table info<tr>
		<td>1@0B</td>
		<td>刘备</td>
		<td>1gao</td>
		<td>11</td>
		<td>9850</td>
	</tr>
</table>
'''
